package cn.diancan.util.print;

import cn.diancan.config.Config;
import cn.diancan.model.types.MealLocationType;
import cn.diancan.util.DoubleArith;
import cn.diancan.util.MoneyUtil;

import javax.imageio.ImageIO;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ProductPrint implements Printable{
	
	private int height=40;
	private int width=220;
	private int x=0;
	private int y=-20;  
	
	public int getHeight(){ 
		return height +draw(null);
	} 

	public int getWidth() { 
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	

	@Override
	public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

		if (page > 0) {
			return NO_SUCH_PAGE;
		}

		Graphics2D g2d = (Graphics2D) g; 
		draw(g2d);
		
		return PAGE_EXISTS;
	}

	private final static String LINE="——————————————————————————————————";


	public String getgetLocationName0(){


		String locationName = getLocationName();
		if(locationName==null || locationName.equals("")){
			return "";
		}


		String locationName2 =Pattern.compile("[^0-9]").matcher(locationName).replaceAll("");
		if(locationName2.length()<=0){
			return locationName.substring(0,1);
		}

		return locationName2;
	}

	public void test33() {
		String phoneString = "哈哈,13888889999";
		// 提取数字
		// 1
		/*Pattern pattern = Pattern.compile("[^0-9]");
		Matcher matcher = pattern.matcher(phoneString);
		String all = matcher.replaceAll("");
		System.out.println("phone:" + all);*/
		// 2
		Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");


	}

	
	
	public int draw(Graphics2D g2d){
		if(null == g2d){
			BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
			g2d =image.createGraphics();
		}


		int fk=0;
		int nowHeight=16;

		g2d.setFont(new Font("Default", Font.PLAIN, 16));

		nowHeight = centerString(this.brandShopName,16,nowHeight,g2d);



		//nowHeight = centerString("#"+ getLocationName()+"#",16,nowHeight,g2d);
		//g2d.drawString(this.brandShopName+"#"+ getLocationName()+"#" , fk+86,  nowHeight=nowHeight+=16);

		nowHeight=nowHeight+5;
		g2d.setFont(new Font("Default", Font.PLAIN, 10));


		//读取第一张图片
		try {
			String path = new ProductPrint().getClass().getClassLoader().getResource("").getPath();
			path= path.substring(0, path.length() - "WEB-INF/classes/".length()) + "images/res/print.png";
			System.out.println(path);

			File  fileOne  =  new File(path);
			BufferedImage  imageOne = ImageIO.read(fileOne);

			int mwidth=50;
			g2d.drawImage(imageOne,fk+70+70 ,nowHeight,mwidth,mwidth,null);
		} catch (IOException e) {
			e.printStackTrace();
		}


		nowHeight=getNow(nowHeight);
		g2d.drawString("单号:", fk,nowHeight);
		g2d.drawString(getOrderNumber(), fk+30,nowHeight);


		g2d.drawOval(fk+60+45 , nowHeight,30,30);

		g2d.setFont(new Font("Default", Font.PLAIN, 16));

		String s = getgetLocationName0();

		if(s.length()>1){
			g2d.drawString(s,fk+50+60,nowHeight+20);
		}else {
			g2d.drawString(s,fk+55+60,nowHeight+20);
		}

		g2d.setFont(new Font("Default", Font.PLAIN, 10));


		nowHeight=getNow(nowHeight);
		g2d.drawString("收银员:", fk, nowHeight );
		g2d.drawString(getShopClerk(), fk+40, nowHeight );

		nowHeight=getNow(nowHeight);
		g2d.drawString("座位:",fk,nowHeight);
		g2d.drawString(getLocationName(),fk+30,nowHeight);


		String orderDate = getOrderDate();
		nowHeight=getNow(nowHeight);
		g2d.drawString("时间:",fk,nowHeight);
		g2d.drawString(orderDate.substring(2,orderDate.length()-3), fk+30,nowHeight);



		//g2d.drawString("VIP:", fk, nowHeight=getNow(nowHeight));
		nowHeight = getNow(nowHeight);
		g2d.drawString("地址:", fk, nowHeight);
		nowHeight =	drawSuperString(getShopAddr(), fk+30, nowHeight-10,11, g2d);

		nowHeight=getNow(nowHeight);

		nowHeight=getNow(nowHeight);

		int fv1 = 115;
		int fv2 = 35;
		int fv3 = 35;

		g2d.drawString("品名", fk, nowHeight);
		g2d.drawString("数量", fk+fv1, nowHeight);
		g2d.drawString("单位", fk+fv1+fv2, nowHeight);
		g2d.drawString("单价", fk+fv1+fv2+fv3, nowHeight);

		boolean b=true;

		nowHeight = productT(productTableVoList,g2d,fk,fv1,fv2,fv3,nowHeight);
		nowHeight = productT(productTableVoList2,g2d,fk,fv1,fv2,fv3,nowHeight);
		nowHeight = productT(productTableVoList3,g2d,fk,fv1,fv2,fv3,nowHeight);
		nowHeight = productT(productTableVoList4,g2d,fk,fv1,fv2,fv3,nowHeight);



		if( productTableVoList3 != null  && productTableVoList3.size() >0 ){
			g2d.drawString(LINE, fk, nowHeight=getNow(nowHeight));
			g2d.drawString("注：酒水无折扣", fk, nowHeight=getNow(nowHeight));
		}


		int p1=40;
		int p2=90;
		int p3=120;

		g2d.drawString(LINE, fk, nowHeight=getNow(nowHeight));
		//nowHeight=getNow(nowHeight);
		//g2d.drawLine(fk, nowHeight, getWidth(), nowHeight);;
		nowHeight=getNow(nowHeight);
		g2d.drawString("共计:", fk, nowHeight);
		g2d.drawString(getGongji(), fk+p1, nowHeight);

		if(null !=discount && !discount.equals(1F)){
			g2d.drawString("折扣:", fk+p2, nowHeight);
			g2d.drawString(getDiscount(), fk+p3, nowHeight);
		}else if(null !=youhui && youhui >0d){
			g2d.drawString("优惠:", fk+p2, nowHeight);
			g2d.drawString(getYouhui(), fk+p3, nowHeight);
		}


		g2d.drawString(LINE, fk, nowHeight=getNow(nowHeight));
		nowHeight=getNow(nowHeight);
		g2d.drawString("付款:", fk, nowHeight);
		g2d.drawString(getFukuan(), fk+p1, nowHeight);
		g2d.drawString("总数:", fk+p2, nowHeight);
		g2d.drawString(getZongshu(), fk+p3, nowHeight);
		g2d.drawString(LINE, fk, nowHeight=getNow(nowHeight));


		if(getLocationType().equals(MealLocationType.OUT.getType())){

			nowHeight=getNow(nowHeight);
			nowHeight =	drawSuperString("客人姓名:"+getCustomerName(), fk , nowHeight-10,18, g2d);

			nowHeight=getNow(nowHeight);
			nowHeight =	drawSuperString("手机号码:"+getCustomerPhone(), fk , nowHeight-10,20, g2d);

			nowHeight=getNow(nowHeight);
			nowHeight =	drawSuperString("客人地址:"+getCustomerAddress(), fk , nowHeight-10,18, g2d);

			nowHeight=getNow(nowHeight);
			nowHeight =	drawSuperString("备注:"+getCustomerRemark(), fk , nowHeight-10,18, g2d);
		}


/*		nowHeight=getNow(nowHeight);
		g2d.drawString("找零:", fk, nowHeight);
		g2d.drawString(getZhaolin(), fk+p1, nowHeight);
		g2d.drawString("积分:", fk+p2, nowHeight);
		g2d.drawString(getJifen(), fk+p3, nowHeight);
		g2d.drawString("—————————————————", fk, nowHeight=getNow(nowHeight));*/

		nowHeight=nowHeight+5;
		g2d.drawString(this.brandShopName2+"祝您用餐愉快", fk, nowHeight=getNow(nowHeight));
		g2d.drawString("订餐热线：88980875", fk, nowHeight=getNow(nowHeight));



		//g2d.drawString(" ", fk, nowHeight=getNow(nowHeight));
		nowHeight=getNow(nowHeight);
		nowHeight=getNow(nowHeight);
		nowHeight=getNow(nowHeight);

		return nowHeight;
	}

	private int productT(List<ProductTableVo> productTableVoList,Graphics2D g2d,int fk,int fv1,int fv2,int fv3 ,int nowHeight){

		if(productTableVoList == null  || productTableVoList.size() <=0){
			return nowHeight;
		}
		int fontSize = 14;

		g2d.drawString(LINE, fk, nowHeight=getNow(nowHeight));
		g2d.setFont(new Font("Default", Font.PLAIN, fontSize));
		for (int i = 0; i < productTableVoList.size(); i++) {
			ProductTableVo vo = productTableVoList.get(i);
			int maxNowHeight =  drawSuperString(vo.getGoodsName(), fk, nowHeight,8, g2d,fontSize+2);
			int maxNowHeight2 = drawSuperString(vo.getQuantity().toString(), fk+fv1, nowHeight,6, g2d,fontSize+2);
			int maxNowHeight3 = drawSuperString(vo.getUnit(), fk+fv1+fv2, nowHeight,4, g2d,fontSize+2);
			int maxNowHeight4 = drawSuperString(vo.getRetailPrice().toString(), fk+fv1+fv2+fv3, nowHeight,6, g2d,fontSize+2);

			nowHeight = maxNowHeight>maxNowHeight2?maxNowHeight:maxNowHeight2;
			nowHeight = nowHeight>maxNowHeight3?nowHeight:maxNowHeight3;
			nowHeight = nowHeight>maxNowHeight4?nowHeight:maxNowHeight4;
		}
		g2d.setFont(new Font("Default", Font.PLAIN, 10));
		return nowHeight;
	}


	public int drawSuperString(String str,int x, int nowHeight, int _long,Graphics2D g2d,int height){
		int maxNowHeight= nowHeight;
		int tempNowHeight =nowHeight;
		int hl = (str.length()/_long)+(str.length()%_long==0?0:1)-1;
		for (int j = 0; j <= hl; j++) {
			tempNowHeight = tempNowHeight+height;
			maxNowHeight  = tempNowHeight>maxNowHeight?tempNowHeight:maxNowHeight;
			g2d.drawString(str.substring(_long*j,j==hl?str.length():_long*(j+1)), x, tempNowHeight);
		}
		return maxNowHeight;
	}
	
	
	public int drawSuperString(String str,int x, int nowHeight, int _long,Graphics2D g2d){
		int maxNowHeight= nowHeight;
		int tempNowHeight =nowHeight;  
		int hl = (str.length()/_long)+(str.length()%_long==0?0:1)-1;
		for (int j = 0; j <= hl; j++) {
			tempNowHeight = getNow(tempNowHeight); 
			maxNowHeight  = tempNowHeight>maxNowHeight?tempNowHeight:maxNowHeight;
			g2d.drawString(str.substring(_long*j,j==hl?str.length():_long*(j+1)), x, tempNowHeight);
		}  
		return maxNowHeight;
	}

	public int centerString(String str, int fontSize, int nowHeigh,  Graphics2D g2d){
		int x = width/2-fontSize*str.length()/2;
		g2d.drawString(str,x,nowHeigh);

		return nowHeigh;
	}


	 
	
	public int getNow(int nowHeight){
		nowHeight+=10;
		return nowHeight;
	}
	public int getLineNow(int nowHeight){
		nowHeight+=2;
		return nowHeight; 
	}
	
	public ProductPrint() {
		super(); 
	}
	 
	
	private List<ProductTableVo> productTableVoList; //主菜
	private List<ProductTableVo> productTableVoList2; //冷盘
	private List<ProductTableVo> productTableVoList3; //酒水
	private List<ProductTableVo> productTableVoList4; //餐具 -其他



	private String orderNumber;
	private String orderDate;
	private String shopNumber;
	private String brandShopName;
 	private String brandShopName2;
	private String shopClerk;
	private String shopAddr;
	private String vip;

	
	private Double gongji;
	private Double youhui;
	private Float discount;
	private Double fukuan;
	private Integer jifen; 
	private Double zhaolin;
	private Integer zongshu;


	private String locationName;
	private Integer locationType;

	private String customerName;
	private String customerPhone;
	private String customerAddress;
	private String customerRemark;




	public ProductPrint(
			String orderNumber, String orderDate, String shopNumber,String brandShopName,String brandShopName2,
			String shopClerk,  String shopAddr, String vip, Double gongji,
			Double youhui,Float discount, Double fukuan, Integer jifen, Double zhaolin,
			Integer zongshu) {
		super();
		this.discount = discount;
		this.orderNumber = orderNumber;
		this.brandShopName = brandShopName;
		this.brandShopName2 =brandShopName2;
		this.orderDate = orderDate;
		this.shopNumber = shopNumber;
		this.shopClerk = shopClerk;

		this.shopAddr = shopAddr;
		this.vip = vip;
		this.gongji = gongji;
		this.youhui = youhui;
		this.fukuan = fukuan;
		this.jifen = jifen;
		this.zhaolin = zhaolin;
		this.zongshu = zongshu;
	}


	public void init2(Integer locationType ,String locationName,String customerName,String customerPhone,String customerAddress,String customerRemark){
		 this.locationType=locationType;
		 this.locationName=locationName;
		 this.customerName =customerName;
		 this.customerPhone = customerPhone;
		 this.customerAddress =customerAddress;
		 this.customerRemark =customerRemark;
	}

	public void init3(List<ProductTableVo> productTableVoList,List<ProductTableVo> productTableVoList2,List<ProductTableVo> productTableVoList3,List<ProductTableVo> productTableVoList4){
		this.productTableVoList = productTableVoList;
		this.productTableVoList2 =productTableVoList2;
		this.productTableVoList3 = productTableVoList3;
		this.productTableVoList4 = productTableVoList4;
	}

	public List<ProductTableVo> getProductTableVoList() {
		if(null ==productTableVoList || productTableVoList.size()<=0){
			return new ArrayList<ProductTableVo>();
		}
		return productTableVoList;
	}

	public List<ProductTableVo> getProductTableVoList2() {
		if(null ==productTableVoList2 || productTableVoList2.size()<=0){
			return new ArrayList<ProductTableVo>();
		}
		return productTableVoList2;
	}



	public String getOrderDate() {
		if(null== orderDate){
			return "";
		}
		return orderDate;
	}

	public String getOrderNumber() {
		if(null== orderNumber){
			return "";
		}
		return orderNumber;
	}
	
	

	public String getShopAddr() {
		if(null== shopAddr){
			return "";
		}
		return shopAddr;
	}

	public String getShopNumber() {
		if(null== shopNumber){
			return "";
		}
		return shopNumber;
	}

	public String getShopClerk() {
		if(null== shopClerk){
			return "";
		}
		return shopClerk;
	}

	public String getVip() {
		if(null== vip){
			return "";
		}
		return vip;
	}

	public String getGongji() {
		if(null== gongji){
			return "";
		}
		return gongji.toString();
	}

	public String getYouhui() {
		if(null== youhui){
			return "";
		}
		return youhui.toString();
	}

	public String getFukuan() {
		if(null== fukuan){
			return "";
		}
		return fukuan.toString();
	}

	public String getJifen() {
		if(null== jifen){
			return "";
		}
		return jifen.toString();
	}

	public String getZhaolin() {
		if(null== zhaolin){
			return "";
		}
		return zhaolin.toString();
	}

	public String getZongshu() {
		if(null== zongshu){
			return "";
		}
		return zongshu.toString();
	}


	public String getDiscount() {
		if(null== discount){
			return "";
		}
		return Double.valueOf(DoubleArith.round(Double.valueOf(discount*10) ,1)).toString() ;
	}


	public String getLocationName() {
		if(null== locationName){
			return "";
		}
		return locationName;
	}

	public Integer getLocationType() {
		if(null == locationType){
			return 1;
		}
		return locationType;
	}

	public String getCustomerName() {
		if(null== customerName){
			return "";
		}
		return customerName;
	}

	public String getCustomerPhone() {
		if(null== customerPhone){
			return "";
		}
		return customerPhone;
	}

	public String getCustomerAddress() {
		if(null== customerAddress){
			return "";
		}
		return customerAddress;
	}

	public String getCustomerRemark() {
		if(null== customerRemark){
			return "";
		}
		return customerRemark;
	}
}
